package com.xin.beauty.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xin.beauty.pojo.entity.Item;
import com.xin.beauty.pojo.entity.Project;
import com.xin.beauty.pojo.query.ProjectQuery;
import com.xin.beauty.service.IProjectService;
import com.xin.beauty.util.Result;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Xin
 * @since 2025-05-04
 */
@RestController
@RequestMapping("/project")
public class ProjectController {

    @Autowired
    private IProjectService projectService;

    //Restful  GET(查找)、POST(添加)、DELETE(删除)、PUT(修改)
    @GetMapping("/list")
    //@RequestMapping(value = "/list", method = {RequestMethod.GET})
    //@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public Result list(ProjectQuery projectQuery) {
        System.out.println("projectController.list");
        IPage<Project> page = projectService.list(projectQuery);
        return Result.ok(page);

    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Project project = projectService.getById(id);
        return Result.ok(project);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Project project) {
        projectService.save(project);
        return Result.ok("添加成功");
    }

    @PutMapping("/update")
    public Result update(@RequestBody Project project) {
        projectService.updateById(project);
        return Result.ok("修改成功");
    }

    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        projectService.removeById(id);
        return Result.ok("删除成功");
    }

    // /item/deleteById/1,2,3
    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids) {
        projectService.removeBatchByIds(Arrays.asList(ids));
        return Result.ok("删除成功");
    }

    @GetMapping("/selectAll")
    public Result selectAll() {
        return Result.ok(projectService.list());
    }

    @GetMapping("/selectNotAll")
    public Result selectNotAll() {
        return Result.ok(projectService.listNotAll());
    }

    //导出
    @GetMapping("/exportExcel")
    public void exportData(HttpServletResponse response) {
        projectService.exportExcel(response);
    }

    //导入
    @PostMapping("/importExcel")
    public Result importExcel(MultipartFile file) {
        projectService.importExcel(file);
        return Result.ok("导入成功");
    }
}

